package com.escool.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.TbGrado;
import com.escool.model.TbNivel;

import com.escool.model.TbSedePersonalHasRol;


import com.escool.service.EmpRolService;
import com.escool.service.GradosService;
import com.escool.service.NivelesService;
import com.escool.service.RolService;
import com.escool.service.SedeHasPersonalService;
import com.escool.service.VinSedeSeccionService;

@Controller
public class EmpRolController {
	
	@Autowired
	EmpRolService EmpRolService;

	@Autowired
	SedeHasPersonalService SedePersonalService;

	
	@RequestMapping("showEmpRolService")
	public String showEmpRolService(Map<String, Object> map,HttpServletRequest request){
		String id = request.getParameter("id");
		TbSedePersonalHasRol tb = new TbSedePersonalHasRol();
		tb.setTbSedeHasPersonal(SedePersonalService.findTbSedeHasPersonal(Integer.parseInt(id)));
		//tb.setTbSedeHasSeccion(VinSedeSeccionService.findTbSedeHasSeccion(Integer.parseInt(id)));
		map.put("tbEmpRolService", tb);
		map.put("TbSedePersonalHasRolList", EmpRolService.listTbSedePersonalHasRol(tb.getTbSedeHasPersonal().getIdSedeHasPersonal()));
		return "/empRolService";
	}
	
	@RequestMapping(value = "addEmpRolService", method = RequestMethod.POST)
	public String addRoles(@ModelAttribute("tbEmpRolService") @Valid TbSedePersonalHasRol tbEmpRolService, BindingResult result){
		if (!result.hasErrors()) {
			EmpRolService.addTbSedePersonalHasRol(tbEmpRolService);
			return "redirect:/showEmpRolService?id="+tbEmpRolService.getTbSedeHasPersonal().getIdSedeHasPersonal();
		}
		return "";
	}
	
	@RequestMapping(value = "editEmpRolService", method = RequestMethod.POST)
	public String editRoles(@ModelAttribute("tbEmpRolService") @Valid TbSedePersonalHasRol tbEmpRolService, BindingResult result){
		if (!result.hasErrors()) {
			EmpRolService.editTbSedePersonalHasRol(tbEmpRolService);
			return "redirect:/showEmpRolService?id="+tbEmpRolService.getTbSedeHasPersonal().getIdSedeHasPersonal();
		}
		return "";
	}
	
	@RequestMapping(value = "deleteEmpRolService", method = RequestMethod.POST)
	public String delRoles(@ModelAttribute("tbEmpRolService") @Valid TbSedePersonalHasRol tbEmpRolService, BindingResult result){
		if (!result.hasErrors()) {
			EmpRolService.delTbSedePersonalHasRol(tbEmpRolService.getIdSedePersonalHasRol());
			return "redirect:/showEmpRolService?id="+tbEmpRolService.getTbSedeHasPersonal().getIdSedeHasPersonal();
		}
		return "";
	}

}
